package view.rules;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import features.Feature;

import lexicon.Lexicon;
import lexicon.LexiconList;
import managers.ComponentManager;
import managers.LexiconManager;

import net.miginfocom.swing.MigLayout;

public class SpelledOutPanel extends JPanel{
	
	RuleTypePanel ruleTypePanel;
	ModifyTypePanel modifyTypePanel;
	MorphemPanel morphemPanel;
	SpelledOutPanelA spelledOutPanelA;
	SpelledOutPanelB spelledOutPanelB;
	SpelledOutPanelC spelledOutPanelC;
	SpelledOutPanelD spelledOutPanelD;
	SpelledOutPanelE spelledOutPanelE;
	private JPanel firstPanel;
	private JPanel secondPanel;
	private JButton structButton;
	private int identifier;
	morpheListener morListener;
	
	private LexiconList lexiconList;
	private ArrayList<Feature> featureList;
	private String posFeatures;
	public LexiconList getLexiconList() {
	
		return lexiconList;
	}

	public void setLexiconList(LexiconList lexiconList) {
		this.lexiconList = lexiconList;
		spelledOutPanelA.setList(lexiconList);
		spelledOutPanelC.setLexiconList(lexiconList);
		spelledOutPanelD.setLexiconList(lexiconList);
		spelledOutPanelA.setFormModel(lexiconList);
		spelledOutPanelD.setFormModel(lexiconList);
	}
	
	public void setFeatureList(String posFeatures, String ruleName){
		//this.featureList = features;
		spelledOutPanelA.setPosFeatures(posFeatures, ruleName);
		spelledOutPanelA.setTriggerWordModel(posFeatures);
		spelledOutPanelD.setPosFeatures(posFeatures, ruleName);
		spelledOutPanelC.setPosFeatures(posFeatures, ruleName);
	}

	public SpelledOutPanel(){
		this.setLayout(new MigLayout());
		initComponent();
		addComponent();

	}
	
	private void initComponent(){
		
		morListener = new morpheListener();
		
		firstPanel = new JPanel();
		ruleTypePanel = new RuleTypePanel();
		structButton = new JButton("Structure");
		modifyTypePanel = new ModifyTypePanel();
		morphemPanel = new MorphemPanel();
		
		Handler handler = new Handler();
		
		ruleTypePanel.simpleRButton.addActionListener(handler);
		ruleTypePanel.tableRButton.addActionListener(handler);
		ruleTypePanel.morphoRButton.addActionListener(handler);
		ruleTypePanel.formSelectRButton.addActionListener(handler);
		ruleTypePanel.phraseBuildRButton.addActionListener(handler);
		
		secondPanel = new JPanel();
		secondPanel.setLayout(new MigLayout());
		spelledOutPanelA = new SpelledOutPanelA();
		spelledOutPanelB = new SpelledOutPanelB();
		spelledOutPanelC = new SpelledOutPanelC();
		spelledOutPanelD = new SpelledOutPanelD();
		spelledOutPanelE = new SpelledOutPanelE();
		
		secondPanel.add(spelledOutPanelA);
		ruleTypePanel.simpleRButton.setSelected(true);
		identifier = 1;
		spelledOutPanelA.setVisible(true);
		spelledOutPanelB.setVisible(false);
		spelledOutPanelC.setVisible(false);
		spelledOutPanelD.setVisible(false);
		spelledOutPanelE.setVisible(false);
		
		modifyTypePanel.prefixRButton.setSelected(true);
		
		modifyTypePanel.prefixRButton.addActionListener(morListener);
		modifyTypePanel.suffixRButton.addActionListener(morListener);
		modifyTypePanel.infixRButton.addActionListener(morListener);
		modifyTypePanel.circumfixRButton.addActionListener(morListener);
		modifyTypePanel.newTransRButton.addActionListener(morListener);
		modifyTypePanel.addWordRButton.addActionListener(morListener);
			
	}
	
	private void addComponent(){
		
		addToFirstPanel();
		add(firstPanel, "wrap");
		add(secondPanel);
		
	}
	
	private void addToFirstPanel(){
		firstPanel.add(ruleTypePanel);
		firstPanel.add(structButton);
		structButton.setVisible(true);
		firstPanel.add(modifyTypePanel);
		modifyTypePanel.setVisible(true);
		firstPanel.add(morphemPanel);
		morphemPanel.setVisible(false);
	}
	
	private class Handler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if (e.getSource() == ruleTypePanel.simpleRButton && spelledOutPanelA.isVisible() == false){
				
				removePanel(identifier);
				structButton.setVisible(true);
				modifyTypePanel.setVisible(true);
				morphemPanel.setVisible(false);
				secondPanel.add(spelledOutPanelA);
				identifier = 1;
				spelledOutPanelA.setVisible(true);
				spelledOutPanelB.setVisible(false);
				spelledOutPanelC.setVisible(false);
				spelledOutPanelD.setVisible(false);
				spelledOutPanelE.setVisible(false);
				
				modifyTypePanel.prefixRButton.setSelected(true);
				
				modifyTypePanel.prefixRButton.addActionListener(morListener);
				modifyTypePanel.suffixRButton.addActionListener(morListener);
				modifyTypePanel.infixRButton.addActionListener(morListener);
				modifyTypePanel.circumfixRButton.addActionListener(morListener);
				modifyTypePanel.newTransRButton.addActionListener(morListener);
				modifyTypePanel.addWordRButton.addActionListener(morListener);
			}
			
			if (e.getSource() == ruleTypePanel.tableRButton && spelledOutPanelB.isVisible() == false){
				
				removePanel(identifier);
				structButton.setVisible(true);
				modifyTypePanel.setVisible(true);
				morphemPanel.setVisible(false);
				secondPanel.add(spelledOutPanelB);
				identifier = 2;
				spelledOutPanelA.setVisible(false);
				spelledOutPanelB.setVisible(true);
				spelledOutPanelC.setVisible(false);
				spelledOutPanelD.setVisible(false);
				spelledOutPanelE.setVisible(false);
			}

			if (e.getSource() == ruleTypePanel.morphoRButton && spelledOutPanelC.isVisible() == false){
	
				removePanel(identifier);
				structButton.setVisible(false);
				modifyTypePanel.setVisible(false);
				morphemPanel.setVisible(true);
				secondPanel.add(spelledOutPanelC);
				identifier = 3;
				spelledOutPanelA.setVisible(false);
				spelledOutPanelB.setVisible(false);
				spelledOutPanelC.setVisible(true);
				spelledOutPanelD.setVisible(false);
				spelledOutPanelE.setVisible(false);
				
				morphemPanel.prefixRButton.setSelected(true);
				
				morphemPanel.prefixRButton.addActionListener(morListener);
				morphemPanel.suffixRButton.addActionListener(morListener);
				morphemPanel.inPrefixRButton.addActionListener(morListener);
				morphemPanel.inSuffixRButton.addActionListener(morListener);
			}
			
			if (e.getSource() == ruleTypePanel.formSelectRButton && spelledOutPanelD.isVisible() == false){
				
				removePanel(identifier);
				structButton.setVisible(false);
				modifyTypePanel.setVisible(false);
				morphemPanel.setVisible(false);
				secondPanel.add(spelledOutPanelD);
				identifier = 4;
				spelledOutPanelA.setVisible(false);
				spelledOutPanelB.setVisible(false);
				spelledOutPanelC.setVisible(false);
				spelledOutPanelD.setVisible(true);
				spelledOutPanelE.setVisible(false);
				
			}
			
			if (e.getSource() == ruleTypePanel.phraseBuildRButton && spelledOutPanelE.isVisible() == false){
				
				removePanel(identifier);
				structButton.setVisible(false);
				modifyTypePanel.setVisible(false);
				morphemPanel.setVisible(false);
				secondPanel.add(spelledOutPanelE);
				identifier = 5;
				spelledOutPanelA.setVisible(false);
				spelledOutPanelB.setVisible(false);
				spelledOutPanelC.setVisible(false);
				spelledOutPanelD.setVisible(false);
				spelledOutPanelE.setVisible(true );
				
			}

		}
		
		private void removePanel(int identifier){
			
			switch (identifier){
			
				case 1: secondPanel.remove(spelledOutPanelA);
						break;
				case 2: secondPanel.remove(spelledOutPanelB);
						break;
				case 3: secondPanel.remove(spelledOutPanelC);
						break;
				case 4: secondPanel.remove(spelledOutPanelD);
						break;
				case 5: secondPanel.remove(spelledOutPanelE);
						break;
				
			}
			
		}
		
	}
	
	private class morpheListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if (e.getSource() == morphemPanel.prefixRButton){
				spelledOutPanelC.prefixPanel.setPanel("prefix");
				spelledOutPanelC.inputSuffixPanel.setPanel("prefix");	
				morphemPanel.setPanel("prefix");
			}			
			
			if (e.getSource() == morphemPanel.suffixRButton){				
				spelledOutPanelC.prefixPanel.setPanel("suffix");
				spelledOutPanelC.inputSuffixPanel.setPanel("suffix");
				morphemPanel.setPanel("suffix");
			}
			
			if (e.getSource() == morphemPanel.inPrefixRButton){
				spelledOutPanelC.prefixPanel.setPanel("infixPrefix");
				spelledOutPanelC.inputSuffixPanel.setPanel("infixPrefix");
				morphemPanel.setPanel("infixPrefix");
			}
			
			if (e.getSource() == morphemPanel.inSuffixRButton){
				spelledOutPanelC.prefixPanel.setPanel("infixSuffix");
				spelledOutPanelC.inputSuffixPanel.setPanel("infixSuffix");
				morphemPanel.setPanel("infixSuffix");
			}
			
			if (e.getSource() == modifyTypePanel.prefixRButton){
				spelledOutPanelA.setComponent("prefix");
			}
			
			if (e.getSource() == modifyTypePanel.suffixRButton){
				spelledOutPanelA.setComponent("suffix");
			}
			
			if (e.getSource() == modifyTypePanel.infixRButton){
				spelledOutPanelA.setComponent("infix");
			}
			
			if (e.getSource() == modifyTypePanel.circumfixRButton){
				spelledOutPanelA.setComponent("circum");
			}
			if (e.getSource() == modifyTypePanel.newTransRButton){
				spelledOutPanelA.setComponent("newWord");
			}
			if (e.getSource() == modifyTypePanel.addWordRButton){
				spelledOutPanelA.setComponent("addWord");
			}
		}
		
	}

}
